package cn.stylefeng.guns.modular.quan.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.quan.entity.Coupon;
import cn.stylefeng.guns.modular.quan.mapper.CouponMapper;
import cn.stylefeng.guns.modular.quan.model.params.CouponParam;
import cn.stylefeng.guns.modular.quan.model.result.CouponResult;
import  cn.stylefeng.guns.modular.quan.service.CouponService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Dong
 * @since 2023-10-31
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {

    @Override
    public void add(CouponParam param){
        Coupon entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CouponParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CouponParam param){
        Coupon oldEntity = getOldEntity(param);
        Coupon newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CouponResult findBySpec(CouponParam param){
        return null;
    }

    @Override
    public List<CouponResult> findListBySpec(CouponParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CouponParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Object getCouponList(String userId) {
        return this.baseMapper.getCouponLists(userId);
    }

    @Override
    public Object getCouponDetail(String userId, Long couponId) {
        return this.baseMapper.getCouponDetail(userId,couponId);
    }

    @Override
    public int jianquan(Long couponId) {
        return this.baseMapper.jianquan(couponId);
    }

    private Serializable getKey(CouponParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Coupon getOldEntity(CouponParam param) {
        return this.getById(getKey(param));
    }

    private Coupon getEntity(CouponParam param) {
        Coupon entity = new Coupon();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
